<template>
<d2-container>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
           style="width: 30%;">

    <el-form-item label="昵称" prop="username">
      <el-input v-model="ruleForm.username"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="ruleForm.nickname"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input type="password" v-model="ruleForm.pwd" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPwd">
      <el-input type="password" v-model="ruleForm.checkPwd" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="权限" prop="roles">
      <el-select v-model="ruleForm.roles" placeholder="请选择">
        <el-option
          v-for="item in role_options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="电话" prop="phone">
      <el-input type="text" v-model="ruleForm.phone"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="email">
      <el-input v-model="ruleForm.email"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="ruleForm.remark"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</d2-container>
</template>

<script>
const axios = require('axios');
import {request} from "@/network/request";

export default {
  name: 'create',
  data() {
    let checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      }
      setTimeout(() => {
        if (value === null) {
          callback(new Error('请输入号码'));
        } else {
          if (value.toString().length !== 11) {
            callback(new Error('号码有误，请检查后修改'));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPwd !== '') {
          this.$refs.ruleForm.validateField('checkPwd');
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: 'front_test',
        nickname: '前端测试',
        pwd: 'abc123456',
        checkPwd: 'abc123456',
        roles: 'ROLE_user',
        phone: '15066686275',
        email: 'abc@gmail.com',
        remark: '无'
      },role_options: [{
        value: 'ROLE_admin',
        label: '管理员'
      }, {
        value: 'ROLE_user',
        label: '用户'
      }, {
        value: 'ROLE_staff',
        label: '职工'
      }],
      rules: {
        nickname: [
          {required: true, message: '昵称', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入昵称', trigger: 'blur'}
        ],
        pwd: [
          {required: true, validator: validatePass, trigger: 'blur'}
        ],
        checkPwd: [
          {required: true, validator: validatePass2, trigger: 'blur'}
        ],
        phone: [
          {required: true, validator: checkPhone, trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      let _this = this;
      request({
          url: 'reg',
          method: 'post',
          params: {
            nickname: _this.ruleForm.nickname,
            username: _this.ruleForm.username,
            pwd: _this.ruleForm.pwd,
            roles: _this.ruleForm.roles,
            phone: _this.ruleForm.phone,
            email: _this.ruleForm.email,
            remark: _this.ruleForm.remark
          }
        }, res => {
          alert("添加成功！");
          _this.resetForm(formName);

        }, err => {
          alert("添加失败！");
          _this.resetForm(formName);
        }
      );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
